Ext.define('AmpleHarvest.views.application.main',
{
	extend			: 'Ext.panel.Panel',
	alias			: 'widget.ampleharvest-views-application-main',
	requires		:
	[
	 	'AmpleHarvest.util.utility',
	 	"AmpleHarvest.DATA.Model.gsSolr"
	],
	bubbleEvents	:
	[

	],
	anchor			: '100% 100%',
	autoScroll		: true,
	// PASSED VARIABLES
	m_str_id		: false,	
	
	// MEMBER VARIABLES
	m_str_location	: '/solr-gs-base/',
	m_str_path		: 'app/{id}',
	m_obj_map	: 
			{
         	   	M_STR_FILES : 
 	   				{
         	   			autoload	: true,
 		   				serviceID 	: 'files',
 		   				ah_obj		: 'AmpleHarvest.objects.results.results',
 		   				model		: 'AmpleHarvest.DATA.Model.gsSolr',
 		   				alsoLoad	: {
 		   					title 	: 'uninstall files',
 		   					path	: 'unfiles'
 		   				},
 		   				action	: AmpleHarvest.eventHandler.cWindowEvents.C_STR_EVENT_OPEN_APP2FILE_WINDOW,
 		   				proxy		: {
		    				url			: '',
		    				type		: 'rest',
		    				headers	: {
		    					'X-ProxiedEntitiesChain'	: obj_config.user.dn
		    				},
		    				limitParam	: 'limit',
		    				startParam 	: 'offset',
		    				reader	:	
		    				{
		    					type			: 'json',
		    					totalProperty	: 'totalCount',
		    					root			: 'collection'
		    				}				    				
		    			},
 		   				columns		: [
						               {  header      : 'Title', dataIndex   : 'Name', flex: 1.5, sortable : true,
							                  renderer: function(value, metaData, record) { return "<b>"+value+"</b>" } 
						               },
						               { header : 'Extension', 		dataIndex	: 'Extension',	flex :1 },
						               { header : 'Size', 			dataIndex	: 'Size',		flex :1 },
						               { header : 'Md5', 			dataIndex	: 'Md5',		flex :1 },
						               { header : 'Sha1', 			dataIndex	: 'Sha1',		flex :1 },
						               { header : 'Mime Type', 		dataIndex	: 'Mime Type',	flex :1 },
						               { header : 'Modified', 		dataIndex	: 'Modified',	flex :1,	hidden	: true },
						               { header : 'Deleted', 		dataIndex	: 'Deleted',	flex :1,	hidden	: true },
						               { header : 'Path', 			dataIndex	: 'Path',		flex :1,	hidden	: true }
							],
 		   				location	: 'center',
 		   				childof		: this
 	   				},
         	   	M_OBJ_REGISTRY	:
         	   		{
         	   			autoload	: true,
		         	   	serviceID 	: 'registry',
		   				ah_obj		: 'AmpleHarvest.objects.results.results',
 		   				model		: 'AmpleHarvest.DATA.Model.gsSolr',
 		   				alsoLoad	: {
 		   					title 	: 'uninstall registry',
 		   					path	: 'unregistry'
 		   				},
 		   				proxy		: {
		    				url			: '',
		    				type		: 'rest',
		    				headers	: {
		    					'X-ProxiedEntitiesChain'	: obj_config.user.dn
		    				},
		    				limitParam	: 'limit',
		    				startParam 	: 'offset',
		    				reader	:	
		    				{
		    					type			: 'json',
		    					totalProperty	: 'totalCount',
		    					root			: 'collection'
		    				}				    				
		    			},
		    			columns		: [
						               {  header      : 'Operation', dataIndex   : 'Operation', flex: 1.5, sortable : true,
							                  renderer: function(value, metaData, record) { return "<b>"+value+"</b>" } 
						               },
						               { header : 'Operation Time', 	dataIndex	: 'Operation Time',	flex :1 },
						               { header : 'Path', 				dataIndex	: 'Path',			flex :1 },
						               { header : 'Result', 			dataIndex	: 'Result',			flex :1 },
						               { header : 'summary', dataIndex : 'Details' }
//						               {  header      : 'Details', dataIndex   : 'Operation', flex: 1.5, sortable : true,
//							                  renderer: function(value, metaData, record) { return "<b>"+value+"</b>" } 
//						               },
							],
		   				childof		: this
         	   		},
         	   	M_OBJ_NETWORK :
         	   		{
         	   			autoload	: true,
 		   				serviceID 	: 'network',
 		   				ah_obj		: 'AmpleHarvest.objects.results.results',
 		   				model		: 'AmpleHarvest.DATA.Model.gsSolr',
 		   				alsoLoad	: {
 		   					title 	: 'uninstall network',
 		   					path	: 'unnetwork'
 		   				},
 		   				proxy		: {
		    				url			: '',
		    				type		: 'rest',
		    				headers	: {
		    					'X-ProxiedEntitiesChain'	: obj_config.user.dn
		    				},
		    				limitParam	: 'limit',
		    				startParam 	: 'offset',
		    				reader	:	
		    				{
		    					type			: 'json',
		    					totalProperty	: 'totalCount',
		    					root			: 'collection'
		    				}				    				
		    			},
 		   				columns		: [
						               {  header      : 'Title', dataIndex   : 'Name', flex: 1.5, sortable : true,
							                  renderer: function(value, metaData, record) { return "<b>"+value+"</b>" } 
						               },
						               { header : 'Version', 			dataIndex	: 'Version',			flex :1 },
						               { header : 'Date Downloaded', 	dataIndex	: 'Date Downloaded',		flex :1 },
						               { header : 'Date Profiled', 		dataIndex	: 'Date Profiled',		flex :1 },
						               { header : 'Profiler Version', 	dataIndex	: 'Profiler Version',	flex :1 },
						               { header : 'Source', 			dataIndex	: 'Source',			flex :1 }
							],
 		   				location	: 'south',
 		   				childof		: this
         	   		}
         	},


	listeners		:
	{
		openItem	: function(obj)
		{
			console.log(obj);
			
		},
		
		dataResponse	: function(obj_data)
		{

			this.m_str_title = 'APPLICATION :'+obj_data.Name;
			this.setTitle(this.m_str_title);
			
			m_obj_AmpleHarvest_ViewPort.fireEvent(AmpleHarvest.eventHandler.cWindowEvents.C_STR_EVENT_ADD_ACTION,
					'AmpleHarvest.views.application.main',
					this.m_str_title,
					{
						m_str_id : this.m_str_id
					}
				);
			
			
			var obj_data_items = {};
			obj_data_items.data = new Array();
			var i = 0;
			for(var key in obj_data)
			{
				if(Ext.isString(obj_data[key])&&key!='Description')
				{
					var obj_temp = {};
					obj_temp['Name'] = key;
					obj_temp['Value'] = obj_data[key];
					obj_data_items.data.push(obj_temp);
				}
			}
			
			this.obj_topbox = Ext.create('Ext.panel.Panel',
					{
						layout		: 'hbox',
						flex		: 1
					})
			
			this.obj_fields_store = Ext.create('Ext.data.Store',
					{
						fields : [
						     'Name',
						     'Value'
						],
						data 	: obj_data_items,
						proxy	: {
							type	: 'memory',
							reader	: {
								type	: 'json',
								root	: 'data'
							}
						}
					}
				);
			
			
			this.obj_fields = Ext.create('Ext.grid.Panel',
					{
						title		: 'Details',
						store		: this.obj_fields_store,
						maxHeight	: 300,
						flex		: 1,
						autoScroll	: true,
						columns	: [
						       	   { text : 'Name',		dataIndex : 'Name', flex : 1 },
						       	   { text : 'Value',	dataIndex : 'Value', flex : 3 },
						],
						
						listeners	: {
							itemcontextmenu: function(grid, record, item, index, event, opts)
						    {
								console.log('show context');
								
						    	event.preventDefault();
						    	
						    	var exportMenu = new Ext.menu.Menu({
						    		items: [{
						    			scope: this,
						                text: 'Export',
						                handler: function() {
						                	open(this.m_str_exportPath);
						                }
						    		}]
						    	});
						    	exportMenu.showAt(event.getXY());
						    }
						}	
					}
				)
				
			this.obj_description = Ext.create('Ext.panel.Panel',
					{
						title	: 'Description',
						maxHeight	: 300,
						flex		: 1,
						autoScroll	: true,
						html	: obj_data.Description,
						flex	: 1
					})
				
			this.obj_topbox.add(this.obj_fields);
			this.obj_topbox.add(this.obj_description);
			this.add(this.obj_topbox);
			
			this.obj_items = {};
			var arr_loading_list = [];
			
			for(var key in this.m_obj_map)
			{
				var str_urn = this.m_str_path+'/'+this.m_obj_map[key]['serviceID'];

				if(str_urn)
				{
					var obj_main = this.makePanel(this.m_obj_map[key], str_urn, this.m_obj_map[key]['serviceID']);
					arr_loading_list.push(obj_main);
					
					if(Ext.isObject(this.m_obj_map[key]['alsoLoad']))
					{
						var obj_primary = obj_main; 
						var obj_secondary = this.makePanel(this.m_obj_map[key], this.m_str_path+'/'+this.m_obj_map[key]['alsoLoad']['path'], this.m_obj_map[key]['alsoLoad']['title']);
						arr_loading_list.push(obj_secondary);
						var obj_main = Ext.create('Ext.tab.Panel',
								{
									items : [obj_primary, obj_secondary]
								});
					}
					
					this.obj_items[key] = obj_main;
					this.add(this.obj_items[key]);
				}				
			}
			
			for(var key in arr_loading_list)
			{
				arr_loading_list[key].getStore().load();
			}
		},	
		
		beforedestory	: function(obj_self)
		{
			
			obj_self.m_obj_util.destroy();
		}
	},
	
	initComponent	: function()
	{		
		if(Ext.isObject(this.obj_form_info))
		{
			//TODO remove obj_form_info from the event log and just send in obj_params in the do_AddAction of cWindowEvents
			this.m_str_id = this.obj_form_info.m_str_id;
		}
		if(this.m_str_id)
		{
			var str_unique_id = (((1+Math.random())*0x10000)|0).toString(16).substring(1);
			this.m_obj_util = Ext.create('AmpleHarvest.util.utility');
			this.m_obj_util.AHcaller = this;
			this.m_str_path = this.m_str_location+this.m_str_path.replace('{id}', encodeURIComponent(this.m_str_id));
			
			this.m_obj_util.getData(this.m_str_path, { excludeUrls : true },{},'GET', {}, {});
		} else {
			this.html = "THIS PAGE REQUIRES AN ID as m_str_id";
		}

		this.callParent();
		
	},
	
	makePanel	: function(obj_map_item, str_url, str_title)
	{
		obj_map_item['proxy'].url = str_url;
		return Ext.create(obj_map_item['ah_obj'],
				{
					AHcaller: this,
					title	: str_title,
					flex	: 1,
					height	: 200,
					maxLimit: 25,
					previewField : 'Details',
					store	: Ext.create('Ext.data.Store',
			    			{
								AHcaller	: this,
				    			requires	:
				    				[
				    				 	obj_map_item['model']
				    				],
				    			model			: obj_map_item['model'],
				    			proxy			: obj_map_item['proxy'],
				    			set_ExtraParams	: function(obj_Params)
				    			{
				    				this.proxy.extraParams = obj_Params;
				    			}
			    			}),
			    	columns: obj_map_item['columns']					    	
				}
		);
	}
});


